<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>时间解析测试</title>
    <style>
        body { 
            font-family: Arial, sans-serif; 
            max-width: 800px; 
            margin: 0 auto; 
            padding: 20px;
            background: #f5f5f5;
        }
        .test-case { 
            background: white;
            margin: 10px 0; 
            padding: 15px; 
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }
        .input { 
            font-weight: bold; 
            color: #333;
            margin-bottom: 8px;
        }
        .result { 
            color: #666; 
            font-size: 14px;
        }
        .success { color: #22c55e; }
        .error { color: #ef4444; }
        button {
            background: #3b82f6;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 6px;
            cursor: pointer;
            margin: 10px 5px 10px 0;
        }
        button:hover { background: #2563eb; }
    </style>
</head>
<body>
    <h1>🕒 时间解析功能测试</h1>
    <p>测试新的时分秒解析逻辑是否正确工作</p>
    
    <button onclick="runAllTests()">🚀 运行所有测试</button>
    <button onclick="clearResults()">🗑️ 清空结果</button>
    
    <div id="results"></div>

    <script>
        // 复制TaskCreatePage中的localSmartParse逻辑
        function localSmartParse(text) {
            const now = new Date()
            const today = now.toISOString().split('T')[0]
            
            const result = {
                dueDate: null,
                dueTime: '',
                priority: 'P2',
                category: 'work',
                tags: [],
                notes: ''
            }
            
            // 增强的时间解析逻辑
            // 1. 解析具体时间 (如: "下午3点半", "上午9点30分", "15:30")
            const timeMatch = text.match(/(上午|下午|晚上|早上|中午)?\s*(\d{1,2})(点|时|:)\s*(半|(\d{1,2})(分?)|(\d{2}))?/)
            if (timeMatch) {
                const timePrefix = timeMatch[1] // 上午/下午等
                let hour = parseInt(timeMatch[2])
                let minute = 0
                
                // 处理分钟
                if (timeMatch[4] === '半') {
                    minute = 30
                } else if (timeMatch[5]) {
                    minute = parseInt(timeMatch[5])
                } else if (timeMatch[7]) {
                    minute = parseInt(timeMatch[7]) // 处理 "15:30" 格式
                }
                
                // 处理上午/下午
                if (timePrefix === '下午' || timePrefix === '晚上') {
                    if (hour < 12) hour += 12
                } else if (timePrefix === '上午' && hour === 12) {
                    hour = 0
                }
                
                // 如果没有明确时间前缀，但小时数小于8，假设是下午
                if (!timePrefix && hour < 8 && hour !== 0) {
                    hour += 12
                }
                
                result.dueTime = `${hour.toString().padStart(2, '0')}:${minute.toString().padStart(2, '0')}`
                result.notes += ` 具体时间: ${result.dueTime}`
            }
            
            // 2. 日期解析
            if (/(上午|下午|晚上|早上|中午)\s*\d{1,2}(点|时)/.test(text) || /\d{1,2}:\d{2}/.test(text)) {
                result.dueDate = today
            } else if (/今天/.test(text)) {
                result.dueDate = today
            } else if (/明天/.test(text)) {
                const tomorrow = new Date(now)
                tomorrow.setDate(now.getDate() + 1)
                result.dueDate = tomorrow.toISOString().split('T')[0]
            }
            
            return result
        }

        const testCases = [
            { input: "明天下午3点开会", expected: { dueTime: "15:00", hasDate: true } },
            { input: "今天上午9点半晨练", expected: { dueTime: "09:30", hasDate: true } },
            { input: "下午5点30分写周报", expected: { dueTime: "17:30", hasDate: true } },
            { input: "晚上8点看电影", expected: { dueTime: "20:00", hasDate: true } },
            { input: "早上6点起床", expected: { dueTime: "06:00", hasDate: true } },
            { input: "中午12点吃午饭", expected: { dueTime: "12:00", hasDate: true } },
            { input: "15:30开会", expected: { dueTime: "15:30", hasDate: true } },
            { input: "9:00晨会", expected: { dueTime: "09:00", hasDate: true } },
            { input: "上午10点15分面试", expected: { dueTime: "10:15", hasDate: true } },
            { input: "下午2点半休息", expected: { dueTime: "14:30", hasDate: true } }
        ]

        function runAllTests() {
            const resultsDiv = document.getElementById('results')
            resultsDiv.innerHTML = '<h2>📊 测试结果</h2>'
            
            let passCount = 0
            let totalCount = testCases.length
            
            testCases.forEach((testCase, index) => {
                const result = localSmartParse(testCase.input)
                const testDiv = document.createElement('div')
                testDiv.className = 'test-case'
                
                let passed = true
                let details = []
                
                // 检查时间解析
                if (result.dueTime !== testCase.expected.dueTime) {
                    passed = false
                    details.push(`❌ 时间解析错误: 期望 "${testCase.expected.dueTime}", 实际 "${result.dueTime}"`)
                } else {
                    details.push(`✅ 时间解析正确: "${result.dueTime}"`)
                }
                
                // 检查日期解析
                if (testCase.expected.hasDate && !result.dueDate) {
                    passed = false
                    details.push(`❌ 日期解析错误: 期望有日期, 实际无日期`)
                } else if (testCase.expected.hasDate && result.dueDate) {
                    details.push(`✅ 日期解析正确: "${result.dueDate}"`)
                }
                
                if (passed) passCount++
                
                testDiv.innerHTML = `
                    <div class="input">测试 ${index + 1}: "${testCase.input}"</div>
                    <div class="result ${passed ? 'success' : 'error'}">
                        ${details.join('<br>')}
                        <br><strong>完整结果:</strong> ${JSON.stringify(result, null, 2)}
                    </div>
                `
                
                resultsDiv.appendChild(testDiv)
            })
            
            // 添加总结
            const summaryDiv = document.createElement('div')
            summaryDiv.className = 'test-case'
            summaryDiv.innerHTML = `
                <div class="input">📈 测试总结</div>
                <div class="result ${passCount === totalCount ? 'success' : 'error'}">
                    通过: ${passCount} / ${totalCount} (${Math.round(passCount/totalCount*100)}%)
                </div>
            `
            resultsDiv.appendChild(summaryDiv)
        }

        function clearResults() {
            document.getElementById('results').innerHTML = ''
        }
    </script>
</body>
</html>
